//
//  HZProductGiftListView.m
//  app
//
//  Created by 刘华舟 on 15/6/24.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZProductGiftListView.h"
#import "UIImageView+URL.h"
#import "UILabel+Conveniently.h"

#import "SNGiftModel.h"
#import "SNPromotionModel.h"

static const NSInteger kListViewWidth = 520;
static const NSInteger kShadowViewWidth = 120;
static const NSInteger kProductPictureWidth = 150;
static const NSInteger kGiftPictureWidth = 60;
static const NSInteger kConfirmButtonHeight = 96;
static const NSInteger kTagLabelWidth = 64;
static const NSInteger kTagLabelHeight =32;
static const NSInteger kPriceLabelHeight = 42;
static const NSInteger kSpecLabelHeight = 80;

@interface HZProductGiftListView()

@property (strong, nonatomic) UIView *backView;

@property (strong, nonatomic) UIView *listBackView;

@end

@implementation HZProductGiftListView

#pragma mark init


- (instancetype)initWithGift:(SNGiftModel *)gift promotions:(NSArray *)promotions url:(NSString *)url price:(NSString *)price title:(NSString *)title spec:(NSString *)spec{
    CGRect frame = CGRectOf([UBO_WINDOW fx:UB_LEFT fy:UB_TOP], [UBO_WINDOW fw:UB_WIDTH fh:UB_HEIGHT]);
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = HZ_CLEAR_COLOR;
        self.backView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT fy:UB_TOP],
                                                                   [self.ubo fw:UB_WIDTH fh:UB_HEIGHT])];
        self.backView.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.8f];
        self.backView.alpha = 0.0f;
        [self addSubview:self.backView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissProductGiftView)];
        [self.backView addGestureRecognizer:tapGesture];
        
        //背景视图
        self.listBackView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_RIGHT fy:UB_TOP],
                                                                  [self.ubo uw:kListViewWidth fh:UB_HEIGHT])];
        self.listBackView.backgroundColor = HZ_WHITE_COLOR;
        [self addSubview:self.listBackView];
        
        UIImageView *productAvatar = [[UIImageView alloc] initWithFrame:CGRectOf([self.listBackView.ubo ux:kSizeMallViewMargin uy:kSizeMallViewMargin oy:20],
                                                                                 [self.listBackView.ubo uw:kProductPictureWidth uh:kProductPictureWidth])];
        productAvatar.layer.masksToBounds = YES;
        productAvatar.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
        productAvatar.layer.borderWidth = [productAvatar.ubo ph:1];
        [productAvatar setImageWithURL:url];
        [self.listBackView addSubview:productAvatar];
        
        UILabel *priceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.listBackView.ubo ux:kSizeMallViewMargin*2+kProductPictureWidth uy:kProductPictureWidth/2+kSizeMallViewMargin-kPriceLabelHeight  oy:20],
                                                                      [self.listBackView.ubo fw:UB_WIDTH uw:-3*kSizeMallViewMargin-kProductPictureWidth uh:kPriceLabelHeight])];
        
        priceLabel.textAlignment = NSTextAlignmentLeft;
        priceLabel.textColor = HZ_MALL_MAIN_THEME_ORANGE;
        priceLabel.font = kHZUFont_TitleBar_M;
        priceLabel.text = [NSString stringWithFormat:@"￥%@", price];
        [self.listBackView addSubview:priceLabel];
        
        //商品名称
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectOf([priceLabel.ufo fx:UB_LEFT fy:UB_BOTTOM],
                                                                    [priceLabel.ubo fw:UB_WIDTH uh:kProductPictureWidth/2])];
        
        titleLabel.textAlignment = NSTextAlignmentLeft;
        titleLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
        titleLabel.numberOfLines = 2;
        titleLabel.font = kHZUFont_Tag_L;
        titleLabel.text = title;
        [self.listBackView addSubview:titleLabel];
        [titleLabel labelTextAlignTop];
        
        
        //购买规格
        UILabel *specLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.listBackView.ubo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_TOP uy:kProductPictureWidth+kSizeMallViewMargin oy:20],
                                                                       [self.listBackView.ubo fw:UB_WIDTH uw:-2*kSizeMallViewMargin uh:kSpecLabelHeight])];
        if (spec.length) {
            NSMutableAttributedString *attriStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"已选择  %@", spec]];
            [attriStr addAttribute:NSFontAttributeName value:kHZUFont_Content_M range:NSMakeRange(0, attriStr.length)];
            [attriStr addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_BLACK_COLOR range:NSMakeRange(0, 5)];
            [attriStr addAttribute:NSForegroundColorAttributeName value:HZ_MALL_TEXT_M_LIGHTGRAY_COLOR range:NSMakeRange(5, attriStr.length-5)];
            specLabel.attributedText = attriStr;
            [self.listBackView addSubview:specLabel];
        }
        
        
       
        
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectOf([self.listBackView.ubo fx:UB_LEFT uy:kSizeMallViewMargin+kProductPictureWidth+kSpecLabelHeight oy:20],
                                                                  [self.listBackView.ubo fw:UB_WIDTH ph:1])];
        lineView.backgroundColor = HZ_MALL_M_SEPARATOR_CELL_LINE;
        [self.listBackView addSubview:lineView];
        
        NSInteger giftHeight = 0;
        if (gift) {
            UILabel *tagGiftLabel = [[UILabel alloc] initWithFrame:CGRectOf([lineView.ufo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_BOTTOM uy:2*kSizeMallViewMargin],
                                                                            [lineView.ubo uw:kTagLabelWidth uh:kTagLabelHeight])];
            
            tagGiftLabel.textAlignment = NSTextAlignmentCenter;
            tagGiftLabel.textColor = HZ_WHITE_COLOR;
            tagGiftLabel.font = kHZUFont_Tag_L;
            tagGiftLabel.text =@"赠品";
            tagGiftLabel.backgroundColor = HZ_MALL_MAIN_THEME_M_PURPLE;
            tagGiftLabel.layer.masksToBounds = YES;
            tagGiftLabel.layer.cornerRadius = [self.ubo uw:4];
            [self.listBackView addSubview:tagGiftLabel];
            
            UIImageView *giftAvatar = [[UIImageView alloc] initWithFrame:CGRectOf([tagGiftLabel.ufo fx:UB_RIGHT ux:kSizeMallViewMargin fy:UB_TOP],
                                                                                     [self.listBackView.ubo uw:kGiftPictureWidth uh:kGiftPictureWidth])];
            giftAvatar.layer.masksToBounds = YES;
            giftAvatar.layer.borderColor = HZ_MALL_BORDER_LINE_GRAY_COLOR.CGColor;
            giftAvatar.layer.borderWidth = [productAvatar.ubo ph:1];
            [giftAvatar setImageWithURL:gift.pic];
            [self.listBackView addSubview:giftAvatar];
            
            UILabel *giftLabel = [[UILabel alloc] initWithFrame:CGRectOf([giftAvatar.ufo fx:UB_RIGHT ux:kSizeMallViewMargin fy:UB_TOP],
                                                                      [lineView.ubo fw:UB_WIDTH uw:-4*kSizeMallViewMargin-kTagLabelWidth-kGiftPictureWidth uh:kGiftPictureWidth])];
            
            giftLabel.textAlignment = NSTextAlignmentLeft;
            giftLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
            giftLabel.font = kHZUFont_Content_S;
            giftLabel.numberOfLines = 2;
            giftLabel.text =gift.title;
            [self.listBackView addSubview:giftLabel];
            [giftLabel labelTextAlignTop];
            
            giftHeight = 2*kGiftPictureWidth;
        }
        
        
        
        if (promotions.count) {
            NSInteger index = 0;
            
            UIView *shortlineView = [[UIView alloc] initWithFrame:CGRectOf([lineView.ufo fx:UB_LEFT ux:kSizeMallViewMargin uy:kSizeMallViewMargin*4+kGiftPictureWidth],
                                                                           [lineView.ubo fw:UB_WIDTH uw:-kSizeMallViewMargin  ph:1])];
            shortlineView.backgroundColor = HZ_MALL_M_SEPARATOR_CELL_LINE;
            [self.listBackView addSubview:shortlineView];
            
            giftHeight += kSizeMallViewMargin;
            
            for (SNPromotionModel *model in promotions) {
                
                UILabel *tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([lineView.ufo fx:UB_LEFT ux:kSizeMallViewMargin fy:UB_BOTTOM uy:giftHeight+2*kSizeMallViewMargin+index*kPriceLabelHeight],
                                                                            [lineView.ubo uw:kTagLabelWidth uh:kTagLabelHeight])];
                
                tagLabel.textAlignment = NSTextAlignmentCenter;
                tagLabel.textColor = HZ_WHITE_COLOR;
                tagLabel.font = kHZUFont_Tag_L;
                tagLabel.text =model.name;
                tagLabel.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
                tagLabel.layer.masksToBounds = YES;
                tagLabel.layer.cornerRadius = [self.ubo uw:4];
                [self.listBackView addSubview:tagLabel];
                
                UILabel *promotionLabel = [[UILabel alloc] initWithFrame:CGRectOf([tagLabel.ufo fx:UB_RIGHT ux:kSizeMallViewMargin fy:UB_CENTER uy:-kTagLabelHeight/2],
                                                                                  [lineView.ubo fw:UB_WIDTH uw:-3*kSizeMallViewMargin-kTagLabelWidth uh:kTagLabelHeight])];
                
                promotionLabel.textAlignment = NSTextAlignmentLeft;
                promotionLabel.textColor = HZ_MALL_TEXT_M_LIGHTGRAY_COLOR;
                promotionLabel.font = kHZFont_Content_M;
                
                promotionLabel.text = model.title;
                
                [self.listBackView addSubview:promotionLabel];
                index++;
            }
        }
        
        
        
        UIButton *confirmBtn = [[UIButton alloc] initWithFrame:CGRectOf([self.listBackView.ubo fx:UB_LEFT fy:UB_BOTTOM uy:-kConfirmButtonHeight],
                                                                        [self.listBackView.ubo fw:UB_WIDTH uh:kConfirmButtonHeight])];
        
        confirmBtn.backgroundColor = HZ_MALL_MAIN_THEME_ORANGE;
        [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
        [confirmBtn setTitleColor:HZ_WHITE_COLOR forState:UIControlStateNormal];
        confirmBtn.titleLabel.font = kHZUFont_Content_L;
        [confirmBtn addTarget:self action:@selector(dismissProductGiftView) forControlEvents:UIControlEventTouchUpInside];
        [self.listBackView addSubview:confirmBtn];
    }
    return self;
}

#pragma mark private methom
- (void)showGiftListView{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.4 animations:^{
        self.backView.alpha = 1.0f;
        self.listBackView.frame = CGRectOf([self.ubo fx:UB_LEFT ux:kShadowViewWidth fy:UB_TOP],
                                           [self.ubo uw:kListViewWidth fh:UB_HEIGHT]);
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hideGiftListView{
    [UIView animateWithDuration:0.4 animations:^{
        self.backView.alpha = 0.0f;
        self.listBackView.frame = CGRectOf([self.ubo fx:UB_RIGHT fy:UB_TOP],
                                           [self.ubo uw:kListViewWidth fh:UB_HEIGHT]);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}


#pragma mark event handler
//点击空白界面 视图消失
- (void)dismissProductGiftView{
    [self hideGiftListView];
}

@end
